<?php 

class CmsAction extends AppModel
{
	var $name = 'CmsAction';
	
	//var $useTable = false;
	
	var $fieldProperties = array(
	
		'action_id' => array(
			'type' => 'select', 
			'label' => 'Page', 
			'group' => 'select',
			'empty' => '-Select-',
			'class' => 'action',
			'triggers' => array('onChange' => array('update' => 'filter'))
		),
		'filter' => array(
			'type' => 'select',
			'group' => 'select',
			//'empty' => '-Select-',
			'label' => '&nbsp;',
			'triggers' => array('onChange' => array('update' => 'filter_value')),
			'div' => 'hidden'
		),
		'filter_value' => array(
			'type' => 'multipleSelect',
			'storageModel' => array('Cms.CmsActionParam', 'value'),
			'group' => 'select',
			'label' => '&nbsp;',
			'options' => array(' '),
			'div' => 'hidden',
			//'fieldModifier' => 'toggleSerialized'
		),

	);
	
	function getFilterOptions($action_id)
	{
		$action = GigaActionHandler::get($action_id);
		if(!is_object($action))
		{
			return array();
		}
		$filters = array(null => 'All '.$action->title);
		if($action->isDynamic())
		{
			$urlFilters = $action->getDynamicParams();
			$dynamicParams = array();
			foreach($urlFilters as $option)
			{
				$dynamicParams[$action_id.'--'.$option] = $action->getLabel($option);
			}
			$filters = am($filters, $dynamicParams);
		}
		return $filters;
	}
	
	function getFilterValueOptions($filter)
	{
		if($filter == '')
		{
			return array();
		}	
		
		if(is_array($filter))
		{
			return array();
		}
		
		$filterValues = array();

		list($action_id, $filter) = explode('--', $filter);
		
		$action = GigaActionHandler::get($action_id);
		if(!is_object($action))
		{
			return array();
		}
		if($action->isDynamic())
		{
			$filterValues = am($filterValues, $action->getCombinations($filter));
		}
		return $filterValues;
	}
		
	function afterInitialize()
	{
		$actions = cri('Cms.Sitemap')->getActions('level', 0);
		//$this->fieldProperties['action_id']['options'] = array('*' => 'All pages');
		foreach($actions as $action)
		{
			$action = GigaActionHandler::get($action);
			$this->fieldProperties['action_id']['options'][$action->id] = $action->title;
		}
		//applying __().
		$this->fieldProperties['action_id']['empty'] = __('-Select-');
	}
	
	/*
	function beforeSave()
	{
		$params = array();
		foreach($this->data['UserSelectedAction'] as $field => $val)
		{
			if(strpos($field, 'params_') !== false)
			{
				unset($this->data['UserSelectedAction'][$field]);
				
				$field = str_replace('params_', '', $field);
				$params[$field] = $val;
			}
		}
		$this->data['UserSelectedAction']['params'] = $params;
		prd($this->data);
		return true;
	}*/
}

?>